package com.zhongzhi.serviceimpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhongzhi.bean.Configuration;
import com.zhongzhi.common.Common;
import com.zhongzhi.dao.MeasureDao;
import com.zhongzhi.dao.TypeDao;
import com.zhongzhi.service.MeasureService;
import com.zhongzhi.springboot.pojo.Computers;
import com.zhongzhi.springboot.pojo.Equipment;
import com.zhongzhi.springboot.pojo.Measure;
import com.zhongzhi.springboot.pojo.MeasureShow;
import com.zhongzhi.springboot.pojo.MeasureShow2;
import com.zhongzhi.springboot.pojo.Type;
import com.zhongzhi.util.Bo;
import com.zhongzhi.util.ClientUtil;
import com.zhongzhi.util.JsonResult;

import lombok.extern.log4j.Log4j;

@Log4j
@Service
public class MeasureServiceImpl implements MeasureService{

	@Autowired
	private MeasureDao measureDao;
	@Autowired
	private TypeDao typeDao;
	//增
	@Override
	public Bo insert(Measure measure) {
		log.info("后端业务层:数据管理:新增数据:"+measure);
		JsonResult insertServer = insertServer(measure);
		if (insertServer.getCode()==202) {
			log.error("后端:测试数据:新增失败:"+insertServer.getData().toString());
			return new Bo(false, 500, "存储数据失败", 0,null);
		}else {
			log.info("后端:测试数据:新增成功:");
			return new Bo(false, 200, "存储数据成功", 0,0);
		}
	}

	//改
	@Override
	public Bo update(Measure measure) {
		log.info("后端业务层:数据管理:修改数据:"+measure);
		JsonResult insertServer = updateServer(measure);
		if (insertServer.getCode()==202) {
			log.error("后端:测试数据:修改失败:"+insertServer.getData().toString());
			return new Bo(false, 500, "存储数据失败", 0,null);
		}else {
			log.info("后端:测试数据:修改成功:");
			return new Bo(false, 200, "存储数据成功", 1,insertServer.getData().toString());
		}
	}

	//删
	@Override
	public Bo delete(Integer id) {
		log.info("后端业务层:数据管理:根据id删除数据:"+id);
		if (id<=0) {
			return new Bo(false, 400, "当前id不正确",0,null);
		}else {
			JsonResult insertServer = deleteServer(id);
			if (insertServer.getCode()==202) {
				log.error("后端:测试数据:删除失败:"+insertServer.getData().toString());
				return new Bo(false, 500, "存储数据失败", 0,null);
			}else {
				log.info("后端:测试数据:删除成功:");
				return new Bo(false, 200, "存储数据成功", 1,insertServer.getData().toString());
			}
		}

	}
	//批量删除
	@Override
	public Bo deletes(Integer[] ids) {
		log.info("后端业务层:数据管理:根据id数组删除数据:"+ids);
		if (ids==null || ids.length==0) {
			return new Bo(false, 400, "没有任何数据被选中",0,null);
		}else {
			try {
				int i = measureDao.deletes(ids);
				log.info("后端业务层:数据管理:根据id数组删除数据结果:"+i);
				return new Bo(true, 200, "删除数据成功",1,i);
			} catch (Exception e) {
				log.error("后端业务层:数据管理:根据id数组删除数据失败"+e);
				return new Bo(false, 500, "删除数据失败",1,e);
			}
		}
	}

	//根据机型机号删除
	@Override
	public Bo deleteNum(String type,String num) {
		log.info("后端业务层:数据管理:根据id数组删除数据:"+type+":"+num);
		try {
			JsonResult deleteNumServer = deleteNumServer(type, num);
			log.info("后端业务层:数据管理:根据机型机号删除数据结果:"+deleteNumServer);
			return new Bo(true, 200, "删除数据成功",1,deleteNumServer);
		} catch (Exception e) {
			log.error("后端业务层:数据管理:根据id数组删除数据失败"+e);
			return new Bo(false, 500, "删除数据失败",1,e);
		}

	}
	//查
	@Override
	public Bo select(Integer pages,Integer line,Long startTime,Long endTime,Integer StationId,String str) {
		log.info("后端业务层:数据管理:根据各种条件查询数据:pages="+pages+"line="+line+"startTime="+startTime+"endTime="+endTime+"StationId="+StationId+"str="+str);
		if (pages==null) {
			pages=1;
		}
		if (line==null) {
			line=20;
		}		
		int a = (pages-1)*line;
		try {
			List<MeasureShow> select = measureDao.select(a, line, startTime, endTime, StationId, str);
			log.info("后端业务层:数据管理:根据各种条件查询数据:pages="+pages+"line="
					+line+"startTime="+startTime+"endTime="
					+endTime+"StationId="+StationId+"str="+str+"结果:"+select);
			return new Bo(true, 200, "查询测试数据成功",select.size(),select);
		} catch (Exception e) {
			log.error("后端业务层:数据管理:根据各种条件查询数据失败:"+e);
			return new Bo(false, 500, "查询测试数据失败", 1,e);
		}
	}
	//查询总数
	@Override
	public Bo selectSize(Long startTime,Long endTime,Integer StationId,String str) {
		log.info("后端业务层:数据管理:根据条件查询总数"+"startTime="+startTime+"endTime="+endTime+"StationId="+StationId+"str="+str);
		try {
			int i = measureDao.selectSize(startTime, endTime, StationId, str);
			log.info("后端业务层:数据管理:根据条件查询总数"+"startTime="+startTime+"endTime="+endTime+"StationId="+StationId+"str="+str+"结果:"+i);
			return new Bo(true, 200, "查询测试数据成功",1,i);
		} catch (Exception e) {
			log.error("后端业务层:数据管理:根据条件查询总数失败:"+e);
			return new Bo(false, 500, "查询测试数据失败", 1,e);
		}
	}

	//根据id查询
	@Override
	public Bo selectById(Integer id) {
		log.info("后端业务层:数据管理:根据id查询数据:"+id);
		try {
			MeasureShow measure = measureDao.selectById(id);
			log.info("后端业务层:数据管理:根据id查询数据结果:"+measure);
			return new Bo(true, 200, "查询测试数据成功",1,measure);
		} catch (Exception e) {
			log.error("后端业务层:数据管理:根据id查询数据失败:"+e);
			return new Bo(false, 500, "查询测试数据失败", 1,e);
		}
	}
	//跟据机型机号查询数据
	@Override
	public Bo selectByTypeNum(Integer typeId, String deviceNum) {
		log.info("后端业务层:数据管理:跟据机型机号查询数据:typeId="+typeId+"deviceNum"+deviceNum);
		try {
			List<MeasureShow> list = measureDao.selectByNum(typeId, deviceNum);
			log.info("后端业务层:数据管理:跟据机型机号查询数据:typeId="+typeId+"deviceNum"+deviceNum+"结果:"+list);
			return new Bo(true, 200, "查询测试数据成功",1,list);
		} catch (Exception e) {
			log.error("后端业务层:数据管理:跟据机型机号查询数据失败:"+e);
			return new Bo(false, 500, "查询测试数据失败", 1,e);
		}
	}

	@Override
	public Bo webinsert(Measure measure) {
		log.info("后端:测试数据:服务器新增:"+measure);
		try {
			int a = measureDao.insert(measure);
			log.info("后端:测试数据:服务器新增成功:"+a);
			return new Bo(true, 200, "服务器新增成功",1,a);
		} catch (Exception e) {
			log.error("后端:测试数据:服务器新增失败:"+e);
			return new Bo(false, 500, "服务器新增失败", 1,e);
		}
	}
	@Override
	public Bo webupdate(Measure measure) {
		log.info("后端:测试数据:服务器修改:"+measure);
		try {
			int a = measureDao.update(measure);
			log.info("后端:测试数据:服务器新增修改:"+a);
			return new Bo(true, 200, "服务器新增修改",1,a);
		} catch (Exception e) {
			log.error("后端:测试数据:服务器修改失败:"+e);
			return new Bo(false, 500, "服务器修改失败", 1,e);
		}
	}
	@Override
	public Bo webdelete(Integer id) {
		log.info("后端:测试数据:服务器删除:"+id);
		try {
			int a = measureDao.delete(id);
			log.info("后端:测试数据:服务器删除成功:"+a);
			return new Bo(true, 200, "服务器删除成功",1,a);
		} catch (Exception e) {
			log.error("后端:测试数据:服务器删除失败:"+e);
			return new Bo(false, 500, "服务器删除失败", 1,e);
		}
	}
	@Override
	public Bo webselect() {
		log.info("后端:测试数据:服务器修改:");
		try {
			ArrayList<Measure> list =measureDao.webselect();
			log.info("后端:测试数据:服务器修改成功:"+list);
			return new Bo(true, 200, "服务器修改成功",1,list);
		} catch (Exception e) {
			log.error("后端:测试数据:服务器修改失败:"+e);
			return new Bo(false, 500, "服务器修改失败", 1,e);
		}
	}
	@Override
	public Bo webdeletenum(String type, String num) {
		log.info("后端:测试数据:服务器删除:"+type+":"+num);
		try {
			List<Type> selectByName = typeDao.selectByName(type);
			int[] a = new int[selectByName.size()];
			int j =0;
			for (Type i : selectByName) {
				a[j]=i.getId();
				j++;
			}
			int deleteNum = measureDao.deleteNum(num, a);
			return new Bo(true, 200, "删除成功", 1, deleteNum);

		} catch (Exception e) {
			return new Bo(false, 500, "删除失败", 1, e);
		}
	}


	private JsonResult insertServer(Measure measure) {
		Configuration configuration = Common.getConfiguration();
		String httpString = "http://"+configuration.getServerId()+":"+configuration.getServerProt()+"/doosanRadar/measure/insert?deviceNum="+measure.getDeviceNum().trim()
				+"&typeId="+measure.getTypeId()
				+"&startTime=" + measure.getStartTime()
				+"&endTime=" + measure.getEndTime()
				+"&stationId="+measure.getStationId()
				+"&sinkA=" + measure.getSinkA()
				+"&startAltitudeA=" + measure.getStartAltitudeA()
				+"&sinkB=" + measure.getSinkB()
				+"&startAltitudeB=" + measure.getStartAltitudeB()
				+"&remark=" + measure.getRemark();
		try {
			log.info("后端:测试数据:同步增加:"+httpString);
			String gethttp = ClientUtil.Gethttp(httpString);
			log.info("后端:测试数据:同步增加结果:"+gethttp);
			return  ClientUtil.Jsonobj(gethttp, JsonResult.class);
		} catch (Exception e) {
			try {
				int insert = measureDao.insert(measure);
				log.info("后端:测试数据:同步增加失败,增加本地:"+insert);
				return new JsonResult(201, "", 1, insert);
			} catch (Exception e2) {
				log.error("后端:测试数据:本地增加失败:"+e2);
				return new JsonResult(500, "失败", 0, 0);
			}
		}


	}

	private JsonResult updateServer(Measure measure) {
		Configuration configuration = Common.getConfiguration();
		String httpString = "http://"+configuration.getServerId()+":"+configuration.getServerProt()+"/doosanRadar/measure/update?deviceNum="+measure.getDeviceNum().trim()
				+"&typeId="+measure.getTypeId()
				+"&startTime=" + measure.getStartTime()
				+"&endTime=" + measure.getEndTime()
				+"&stationId="+measure.getStationId()
				+"&sinkA=" + measure.getSinkA()
				+"&startAltitudeA=" + measure.getStartAltitudeA()
				+"&sinkB=" + measure.getSinkB()
				+"&startAltitudeB=" + measure.getStartAltitudeB()
				+"&remark=" + measure.getRemark();
		try {
			log.info("后端:测试数据:同步修改:"+httpString);
			String gethttp = ClientUtil.Gethttp(httpString);
			log.info("后端:测试数据:同步修改结果:"+gethttp);
			return  ClientUtil.Jsonobj(gethttp, JsonResult.class);
		} catch (Exception e) {
			try {
				int insert = measureDao.update(measure);
				log.info("后端:测试数据:同步修改失败,修改本地:"+insert);
				return new JsonResult(201, "", 1, insert);
			} catch (Exception e2) {
				log.error("后端:测试数据:本地修改失败:"+e2);
				return new JsonResult(500, "失败", 0, 0);
			}
		}
	}

	private JsonResult deleteServer(Integer id) {
		Configuration configuration = Common.getConfiguration();
		String httpString = "http://"+configuration.getServerId()+":"+configuration.getServerProt()+"/doosanRadar/measure/delete?id="+id;
		try {
			log.info("后端:测试数据:同步删除:"+httpString);
			String gethttp = ClientUtil.Gethttp(httpString);
			log.info("后端:测试数据:同步删除结果:"+gethttp);
			return  ClientUtil.Jsonobj(gethttp, JsonResult.class);
		} catch (Exception e) {
			try {
				int insert = measureDao.delete(id);
				log.info("后端:测试数据:同步删除失败,删除本地:"+insert);
				return new JsonResult(201, "", 1, insert);
			} catch (Exception e2) {
				log.error("后端:测试数据:本地删除失败:"+e2);
				return new JsonResult(500, "失败", 0, 0);
			}
		}
	}

	public JsonResult deleteNumServer(String type, String num) {
		try {
			Configuration configuration = Common.getConfiguration();
			String httpString = "http://"+configuration.getServerId()+":"+configuration.getServerProt()+"/doosanRadar/measure/deletenum?type="+type+"&num="+num;
			log.info("后端:测试数据:同步删除:"+httpString);
			String gethttp = ClientUtil.Gethttp(httpString);
			log.info("后端:测试数据:同步删除结果:"+gethttp);
			return  ClientUtil.Jsonobj(gethttp, JsonResult.class);

		} catch (Exception e2) {
			log.error("后端:测试数据:本地删除失败:"+e2);
			return new JsonResult(500, "本地删除失败", 0, 0);
		}
	}

	//命令服务器同步数据
	@Override
	public JsonResult insertShow(MeasureShow2 measureShow2) {
		Configuration configuration = Common.getConfiguration();
		String httpString = "http://"+configuration.getServerId()+":"+configuration.getServerProt()+"/doosanRadar/measure/insertshow?deviceNum="+measureShow2.getDeviceNum().trim()
				+"&typeName="+measureShow2.getTypeName()
				+"&startTime=" + measureShow2.getStartTime()
				+"&endTime=" + measureShow2.getEndTime()
				+"&stationId="+measureShow2.getStationId()
				+"&sinkA=" + measureShow2.getSinkA()
				+"&startAltitudeA=" + measureShow2.getStartAltitudeA()
				+"&sinkB=" + measureShow2.getSinkB()
				+"&startAltitudeB=" + measureShow2.getStartAltitudeB()
				+"&remark=" + measureShow2.getRemark();
		try {
			log.info("后端:测试数据:同步增加:"+httpString);
			String gethttp = ClientUtil.Gethttp(httpString);
			log.info("后端:测试数据:同步增加结果:"+gethttp);
			return  ClientUtil.Jsonobj(gethttp, JsonResult.class);
		} catch (Exception e) {
			try {
				Bo webinsertShow = webinsertShow( measureShow2);
				log.info("后端:测试数据:同步增加失败,增加本地:"+webinsertShow.getSize());
				return new JsonResult(201, "", 1, webinsertShow.getSize());
			} catch (Exception e2) {
				log.error("后端:测试数据:本地增加失败:"+e2);
				return new JsonResult(500, "失败", 0, 0);
			}
		}
	}

	//服务器增加数据
	@Override
	public Bo webinsertShow(MeasureShow2 measureShow2) {
		//查询机型名
		List<Type> selectByName = typeDao.selectByName(measureShow2.getTypeName());
		//如果没有则创建机型,然会查询机型
		if (selectByName.size()==0) {
			Type type = new Type();
			type.setTypeName(measureShow2.getTypeName());
			type.setRemarks("此机型测试中自动创建");
			int insert = typeDao.insert(type);
			if (insert==0) {
				return new Bo(false, 400, "创建机型失败", 0,0);
			}
			selectByName = typeDao.selectByName(measureShow2.getTypeName());
		}
		//新增数据
		Type type = selectByName.get(0);
		
		Measure measure = new Measure();
		
		measure.setDeviceNum(measureShow2.getDeviceNum());
		measure.setEndTime(measureShow2.getEndTime());
		measure.setRemark(measureShow2.getRemark());
		measure.setSinkA(measureShow2.getSinkA());
		measure.setSinkB(measureShow2.getSinkB());
		measure.setStartAltitudeA(measureShow2.getStartAltitudeA());
		measure.setStartAltitudeB(measureShow2.getStartAltitudeB());
		measure.setStartTime(measureShow2.getStartTime());
		measure.setStationId(measureShow2.getStationId());
		measure.setTypeId(type.getId());
		
		int insert = measureDao.insert(measure);
		if (insert==0) {
			return new Bo(false, 400, "存储数据失败", 0,0);
		}
		return new Bo(true, 200, "存储数据成功", insert,0);
	}



}
